home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / BLT2_205.ZIP / src / blt2cx06.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-25  |  10.3 KB  |  368 lines

  1. /* 
  2.  *
  3.  * blt2cx06.c - 17-Oct-1995 Cornel Huth 
  4.  * This module is called by blt2demo.c
  5.  * TABLE-MEMO VIEW  (Memo file is dBASE IV/V version)
  6.  * 
  7.  */
  8.  
  9. #include "platform.h"
  10.  
  11. #ifdef ON_OS2
  12.    #include <os2.h>
  13. #endif
  14. #ifdef ON_W95
  15.    #define WIN32_LEAN_AND_MEAN
  16.    #include <windows.h>
  17. #endif
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22. #include <string.h>
  23.  
  24. #ifdef ON_OS2
  25.    #include "bullet2.h"
  26. #endif
  27. #ifdef ON_W95
  28.    #include "bullet95.h"
  29. #endif
  30. #ifdef ON_DOSX
  31.    #include "bulletx.h"
  32. #endif
  33.  
  34. void cx06BuildEmpFieldList(FIELDDESCTYPE fieldList[]);
  35.  
  36. typedef struct _EmpRecType {
  37. CHAR tag;               // record tag, init to SPACE, * means deleted
  38. CHAR empID[9];          // SSN (not 0T string)
  39. CHAR empLN[16];         // last name
  40. CHAR empFN[16];         // first name
  41. CHAR empHire[8];        // "YYYYMMDD" (not 0T string)
  42. CHAR empNotes[10];      // a memo field, called Notes
  43. } EmpRecType; // 60 bytes
  44.  
  45. // Sample data records for the database EMP
  46. // 10-space data at end is the blank memo field value (means no memo yet)
  47.  
  48. EmpRecType empSampleRecords2[] = {
  49. //   123456789   1234567890123456   1234567890123456   12345678   123456
  50. ' ',"465309999","Que",             "Barbie",          "19900131","          ",
  51. ' ',"445038888","Stewart",         "Jackie",          "19910228","          ",
  52. ' ',"760443232","Whitman",         "Kelly",           "19920414","          ",
  53. ' ',"845309944","Beatty",          "Leslie",          "19940122","          ",
  54. ' ',"555033388","Jasper",          "Amy",             "19930230","          ",
  55. ' ',"430443222","Hauntos",         "Poco",            "19920414","          ",
  56. ' ',"365502949","Hopkins",         "Lisa",            "19910121","          ",
  57. ' ',"685733868","Leonard",         "Rosina",          "19850218","          ",
  58. ' ',"500945242","Morton",          "Holly",           "19950406","          ",
  59. ' ',"335209939","Buckly",          "Lois",            "19930715","          ",
  60. ' ',"745338218","Parker",          "Angie",           "19940412","          ",
  61. ' ',"860461892","Sosa",            "Rhoda",           "19940623","          ",
  62. ' ',"225374865","Jefferson",       "Weezie",          "19941106","          ",
  63. ' ',"115036578","Chung",           "Connie",          "19941205","          ",
  64. ' ',"240443355","Baker",           "Rosinda",         "19940304","          ",
  65. };
  66.  
  67. // Sample memos for the database EMP.dbt
  68. // Anything goes
  69.  
  70. CHAR *empSampleMemos[] = {
  71. "This is memo sample number uno",
  72. "This would then be number two",
  73. "And number three",
  74. "Number four, too",
  75. "Don't forget memo number five!",
  76. "Then comes memo six",
  77. "And since seven follows six, memo seven",
  78. "Eight is close to the last memo",
  79. "But nine is even close to the last memo",
  80. "Ten, now this is the last memo",
  81. "Well, no, that wasn't -- this is eleven",
  82. "Memo twelve is this",
  83. "Thirteen here, or is this unlucky?",
  84. "Memo Fourteen, now we're getting some where",
  85. "Memo Fifteen, this is the last (one for each employee record)",
  86. };
  87.  
  88. extern CHAR *collateTable;
  89.  
  90.  
  91. int cx06() {
  92.  
  93. #pragma pack(1)
  94.  
  95. ACCESSPACK AP;
  96. DOSFILEPACK DFP;
  97. CREATEDATAPACK CDP;
  98. CREATEINDEXPACK CIP;
  99. HANDLEPACK HP;
  100. MEMODATAPACK MDP;
  101. OPENPACK OP;
  102.  
  103. CHAR indexFilename[] = "$CX06.IX3";
  104. ULONG indexID=0;
  105.  
  106. CHAR keyExpression[] = "SSN";
  107. CHAR keyBuffer[68];
  108.  
  109. CHAR dataFilename[] = "$CX06.DBF";
  110. ULONG dataID=0;                 
  111.  
  112. // the memo file is created implicitly when the DBF is, but to ease deleting 
  113. // the memo file at program startup, the DBT (as it would be by default)
  114. // is set here
  115.  
  116. CHAR memoFilename[] = "$CX06.DBT";
  117.  
  118. FIELDDESCTYPE empFieldList[5];  // 5 fields used in Employee data record
  119. EmpRecType empRec;
  120.  
  121. #pragma pack()
  122.  
  123. LONG rez;               // return value from Bullet
  124. LONG i;                 // counter
  125. CHAR tmpStr[256];       // misc stuff, non-Bullet related (so far as pack goes)
  126.  
  127. LONG empRecs2Add = sizeof(empSampleRecords2) / sizeof(empRec);
  128.  
  129. printf("Memo file example (sort order is by SSN, not in memo number order)\n\n");
  130.  
  131. // Assign fieldlist members (after first zeroing)
  132.  
  133. memset(empFieldList,0,sizeof(empFieldList));
  134. cx06BuildEmpFieldList(empFieldList);
  135.  
  136. // Delete previous files from any previous run (disregard any error return)
  137.  
  138. DFP.func = DELETE_FILE_DOS;
  139. DFP.filenamePtr = dataFilename;
  140. rez = BULLET(&DFP);
  141. DFP.filenamePtr = memoFilename;
  142. rez = BULLET(&DFP);
  143. DFP.filenamePtr = indexFilename;
  144. rez = BULLET(&DFP);
  145.  
  146. // Create the data files
  147.  
  148. CDP.func = CREATE_DATA_XB;
  149. CDP.filenamePtr = dataFilename;
  150. CDP.noFields = 5;
  151. CDP.fieldListPtr = empFieldList;
  152. CDP.fileID = 0x8B;              // bit7&3=1 then also create memo file
  153. rez = BULLET(&CDP);
  154. if (rez) {
  155.    printf("Failed EMP data file create.  Err: %li\n",rez);
  156.    goto Abend;
  157. }
  158.  
  159. // Open the data file
  160.  
  161. OP.func = OPEN_DATA_XB;
  162. OP.filenamePtr = dataFilename;
  163. OP.asMode = READWRITE | DENYNONE;
  164. rez = BULLET(&OP);
  165. if (rez) {
  166.    printf("Failed EMP data file open.  Err: %li\n",rez);
  167.    goto Abend;
  168. }
  169. dataID = OP.handle;
  170.  
  171. // Create index file
  172.  
  173. CIP.func = CREATE_INDEX_XB;
  174. CIP.filenamePtr = indexFilename;
  175. CIP.keyExpPtr = keyExpression;
  176. CIP.xbLink = dataID;        
  177. CIP.sortFunction = ASCII_SORT;
  178. CIP.codePage = CODEPAGE;
  179. CIP.countryCode = CTRYCODE;
  180. CIP.collatePtr = NULL;
  181. CIP.nodeSize = 512;
  182. rez = BULLET(&CIP);
  183. if (rez) {
  184.    printf("Failed EMP SSN index file create.  Err: %li\n",rez);
  185.    goto Abend;
  186. }
  187.  
  188. // Open the index file
  189.  
  190. OP.func = OPEN_INDEX_XB;
  191. OP.filenamePtr = indexFilename;
  192. OP.asMode = READWRITE | DENYNONE;
  193. OP.xbLink = dataID;
  194. rez = BULLET(&OP);
  195. if (rez) {
  196.    printf("Failed SSN index file open.  Err: %li\n",rez);
  197.    goto Abend;
  198. }
  199. indexID = OP.handle;
  200.  
  201. // Insert into the EMP data file, adding the memo record in the process
  202.  
  203. AP.func = INSERT_XB;
  204. AP.handle = indexID;
  205. AP.keyPtr = keyBuffer;
  206. AP.nextPtr = NULL;
  207.  
  208. MDP.func = ADD_MEMO_XB;
  209. MDP.dbfHandle = dataID;
  210.  
  211. for (i=0;i < empRecs2Add;i++) {
  212.  
  213.    // it may seem odd to add a memo before its DBF record exists, but
  214.    // this is not so odd -- if you were adding a new employee record,
  215.    // say, you'd enter the info via some form of dialog, getting the
  216.    // standard record info (DBF field data) and also any memo data 
  217.    // (comments, other non-field data), and then post that data to the
  218.    // database.  Since the DBF record's memo field needs the memo number
  219.    // (which is returned from the ADD_MEMO_XB call), it only makes sense
  220.    // to first add the memo, then fill in the DBF field's memo number
  221.    // and then INSERT_XB the employee record (DBF, the DBT having already
  222.    // been written to disk) -- and that is what this does
  223.  
  224.    MDP.memoPtr = empSampleMemos[i];
  225.    MDP.memoBytes = strlen(empSampleMemos[i])+1;  // +1 so it stores \0, too
  226.    rez = BULLET(&MDP);
  227.  
  228.    if (rez!=0) {
  229.       printf("ADD_MEMO_XB #%ld failed, err: %ld\n",i,MDP.stat);
  230.       goto Abend;
  231.    } 
  232.  
  233.    sprintf(tmpStr,"%10.10u",MDP.memoNo);  // "0000000001" is first memo...
  234.    strncpy(empSampleRecords2[i].empNotes,tmpStr,10);  // put memo number in
  235.  
  236.    AP.recNo = 0;
  237.    AP.recPtr = &empSampleRecords2[i];
  238.  
  239.    rez = BULLET(&AP);
  240.    if (rez!=0) {
  241.       if (rez < 0) {
  242.          rez = abs(rez);
  243.          printf("INSERT_XB #%ld failed, data pack# %ld, err: %ld\n",
  244.                  i,rez,AP.stat);
  245.       } 
  246.       else {
  247.          printf("INSERT_XB #%ld failed, index pack# %ld, err: %ld\n",
  248.                  i,rez,AP.stat);
  249.       }
  250.       goto Abend;
  251.    }
  252. }
  253.  
  254. // Shows a view on the EMP table, along with memo notes (first 30 or so bytes)
  255. // This is just a crude output method -- the memo routines do return the 
  256. // size of each memo data, and so on -- enough info to manage them quite well,
  257. // much better than can be done in dBASE
  258.  
  259. AP.func = GET_FIRST_XB;
  260. AP.handle = indexID;      
  261. AP.recPtr = &empRec;
  262. AP.keyPtr = keyBuffer;
  263.  
  264. MDP.func = GET_MEMO_XB;
  265. MDP.memoPtr = tmpStr;     // read memo into this (other MDP. parms already set)
  266. MDP.memoOffset = 0;
  267.  
  268. i=0;
  269. //       123456789 1234567890123456 1234567890123456 12345678 123456789012345...
  270. printf(" EMP.SSN   LNAME            FNAME            HIRED    NOTES (memo field)\n");
  271. rez=BULLET(&AP);
  272. while (rez==0) {
  273.  
  274.    // don't want to use atol(empRec.empNotes) since this field is not zero-terminated
  275.    // and fills the 10 bytes completely -- use sscanf(), or atol() on left 10 bytes
  276.    // MDP.memoNo = atol(empRec.empNotes); // scratch this
  277.  
  278.    sscanf(empRec.empNotes,"%10u",&MDP.memoNo);
  279.  
  280.    MDP.memoBytes = 20;
  281.    rez = BULLET(&MDP);
  282.    if (rez!=0) {
  283.       printf("GET_MEMO_XB #%ld failed, err: %ld\n",
  284.               MDP.memoNo,MDP.stat);
  285.       goto Abend;
  286.    }      
  287.  
  288.    printf(" %9.9s %-16.16s %-16.16s %8.8s %-16.16s %-27.27...\n",
  289.            empRec.empID,
  290.            empRec.empLN,
  291.            empRec.empFN,
  292.            empRec.empHire,
  293.            tmpStr);
  294.  
  295.    if (rez==0) {
  296.       i++;
  297.       AP.func = GET_NEXT_XB;
  298.       rez=BULLET(&AP);
  299.    }
  300. }
  301. if (rez==EXB_END_OF_FILE) rez=0; // expected is EXB_END_OF_FILE
  302. if (rez) {
  303.    printf("(SSN) Failed EMP view #%ld, err: %ld\n",i,rez);
  304.    goto Abend;
  305. }
  306.  
  307. // Fatal errors above come straight to here
  308. Abend:
  309.  
  310. // Close files
  311. // closing the data file closes its memo file
  312.  
  313. HP.func = CLOSE_INDEX_XB;
  314. if (indexID) {
  315.    HP.handle = indexID;
  316.    rez = BULLET(&HP);
  317.    if (rez)
  318.       printf("Failed index #%ld file close.  Err: %li\n",i,rez);
  319. }
  320.  
  321. HP.func = CLOSE_DATA_XB;
  322. if (dataID) {
  323.    HP.handle = dataID;
  324.    rez = BULLET(&HP);
  325.    if (rez)
  326.       printf("Failed data #%ld file close.  Err: %li\n",i,rez);
  327. }
  328.  
  329. return rez;
  330.  
  331. }
  332.  
  333.  
  334. ///////////////////////////////////////////////
  335. //
  336. // Init field list items for employee data file
  337. //
  338. ///////////////////////////////////////////////
  339.  
  340. void cx06BuildEmpFieldList(FIELDDESCTYPE fieldList[]) {
  341.  
  342. strcpy(fieldList[0].fieldName, "SSN");
  343. fieldList[0].fieldType = 'C';
  344. fieldList[0].fieldLen = 9;
  345. fieldList[0].fieldDC = 0;
  346.  
  347. strcpy(fieldList[1].fieldName, "LNAME");
  348. fieldList[1].fieldType = 'C';
  349. fieldList[1].fieldLen = 16;
  350. fieldList[1].fieldDC = 0;
  351.  
  352. strcpy(fieldList[2].fieldName, "FNAME");
  353. fieldList[2].fieldType = 'C';
  354. fieldList[2].fieldLen = 16;
  355. fieldList[2].fieldDC = 0;
  356.  
  357. strcpy(fieldList[3].fieldName, "HIRED");
  358. fieldList[3].fieldType = 'D';
  359. fieldList[3].fieldLen = 8;
  360. fieldList[3].fieldDC = 0;
  361.  
  362. strcpy(fieldList[4].fieldName, "NOTES");
  363. fieldList[4].fieldType = 'M';
  364. fieldList[4].fieldLen = 10;
  365. fieldList[4].fieldDC = 0;
  366. }
  367.  
  368.